package com.jy.novel.dao.system;

import com.jy.novel.common.JdbcHelper;
import com.jy.novel.pojo.system.MenuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by jihb on 5/4/16.
 */
@Repository
public class RoleMenuDao {

    @Autowired
    JdbcHelper jdbcHelper;

    /**
     * 根据角色查询菜单 不分页
     * @param roleId
     * @return
     */
    public List<MenuInfo> findMenuListByRole(long roleId){
        StringBuilder sb = new StringBuilder();
        sb.append("select * from system_menu where menuId in(");
        sb.append("select menuId from system_menu_role where roleId=? ");
        sb.append(") or parentId=0 ");
        return jdbcHelper.queryForListBean(sb.toString(),new Object[]{roleId},MenuInfo.class);
    }

    /**
     * 添加角色菜单
     * @param roleId
     * @param menuId
     * @return
     */
    public boolean addRoleMenu(long roleId,long menuId){
        String sql = "insert into system_menu_role(menuId,roleId) values(?,?)";
        return jdbcHelper.update(sql,new Object[]{menuId,roleId})>0;
    }

    /**
     * 删除角色菜单
     * @param roleId
     * @param menuId
     * @return
     */
    public boolean deleteRoleMenu(long roleId,long menuId){
        String sql = "delete from system_menu_role where menuId=? and roleId=? ";
        return jdbcHelper.update(sql,new Object[]{menuId,roleId})>0;
    }

    /**
     * 删除角色的所有菜单
     * @param roleId
     * @return
     */
    public boolean deleteRoleAllMenu(long roleId){
        String sql = "delete from system_menu_role where roleId=? ";
        return jdbcHelper.update(sql,new Object[]{roleId})>0;
    }
}
